package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.pojo.SysUser;
import com.itheima.service.RoleService;
import com.itheima.utils.MD5Utils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：daqiang
 * @date ：Created in 2019/12/1
 * @description ：
 * @version: 1.0
 */

@RestController
@RequestMapping("/role")
public class RoleController {

    @Reference
    RoleService roleService;

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<Role> roleList = roleService.findAll();
            return new Result(true, MessageConst.QUERY_ROLE_SUCCESS,roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.pageQuery(queryPageBean);
    }

    @RequestMapping("/add")
    public Result add(Integer[] permissionIds , Integer[] menuIds ,@RequestBody Role role){
        try {
            roleService.add(role,permissionIds,menuIds);
            return new Result(true, MessageConst.ADD_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ADD_ROLE_FAIL);
        }
    }
//
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            Role role = roleService.findById(id);
            return new Result(true, MessageConst.QUERY_ROLE_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.QUERY_ROLE_FAIL);
        }
    }
//
    @RequestMapping("/findPermissionIdsById")
    public Result findPermissionIdsById(Integer id){
        try {
            List<Integer> permissionIds = roleService.findPermissionIdsById(id);
            return new Result(true,MessageConst.QUERY_ROLE_SUCCESS, permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/findMenuIdsById")
    public Result findMenuIdsById(Integer id){
        try {
            List<Integer> menuIds = roleService.findMenuIdsById(id);
            return new Result(true,MessageConst.QUERY_ROLE_SUCCESS, menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_ROLE_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(Integer[] permissionIds , Integer[] menuIds , @RequestBody Role role){
        try {
            roleService.edit(role, permissionIds,menuIds);
            return new Result(true,MessageConst.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_ROLE_FAIL);
        }
    }

    @RequestMapping("/delById")
    public Result delById(Integer id){
        try {
            roleService.delById(id);
            return new Result(true,MessageConst.DELETE_ROLE_SUCCESS);
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_ROLE_FAIL);
        }
    }
}
